import { Component, Input, OnInit, OnDestroy } from '@angular/core';

import { ProductMonthHistoryService } from './product.month.history.service';

import { Util } from '../../../../utils/util';

@Component({
  selector: 'product-month-history',
  templateUrl: './product.month.history.html',
  providers: [ProductMonthHistoryService]
})

export class ProductMonthHistoryCompoenet {

  @Input() date: any;
  productInfo: any;
  constructor(private service: ProductMonthHistoryService, private util: Util) {
    this.productInfo = this.util.sessionStore().get('productDetail');
  }

  vm = {
    "loading": true,
    "data": [],
    "column": {
      fileName: '',
      dim: ['date', 'monthSellMoney', 'monthSellAmount', 'monthPrice', 'propertyValue', 'brandName', 'uid', 'numberId'],
      dimText: ['月份', '销售额', '销售量', '成交均价', '属性', '品牌名', '店铺id', '宝贝id']
    },
    "type": 'line-month'
  };
  
  columns = ['月份', '销售量', '销售额','成交均价'];


  ngOnInit() { }
  // change

  ngOnChanges(changes) {

    console.log(changes);

    if (changes.date.currentValue && changes.date.currentValue.timeType == 'month') {

      console.log(this.date);
      this.vm.loading = true;
      this.getHttp(this.productInfo, this.date.date);
    }


  }

  ngOnDestroy() {
    if (this.offEvenet) {
      this.offEvenet.unsubscribe();

    }
  }

  // 导出
  export() {
    console.log("export");

    let data = this.util.jsonToArrry(this.vm.data, this.vm.column.dim, this.vm.column.dimText);

    this.util.export(data, this.vm.column.fileName);
  }



  monthSellResulet: any;
  offEvenet: any;
  getHttp(productInfo, date) {

    if(this.vm.loading && this.offEvenet){
      this.offEvenet.unsubscribe();
    }

    let json = {
      numberId: productInfo.numberId,
      uid: productInfo.uid
    };

    Object.assign(json, date);

    this.monthSellResulet = {
      'monthSellMoney': 0,
      'monthSellAmount': 0
    };

    this.vm.column.fileName = this.productInfo.title + date.begin.slice(0, 10) + '~' + date.end.slice(0, 10) + this.productInfo.title; +"月数据";

    var res = {"code":0,"msg":"成功","data":[{"id":"37159234393\u00012017-11","date":"2017-11","crawlDate":"2017-11-11 00:00:00","numberId":37159234393,"monthSellAmount":2680,"monthSellMoney":1334640.0,"monthPrice":498.0,"category":1205,"uid":1974623778,"shopType":"商城","title":"【正品联保】Beats URBEATS 2.0重低音降噪入耳式耳机魔音B耳塞式","loc":"浙江杭州","propertyValue":"是否线控:是,品牌:Beats,耳机插头类型:直插型,套餐类型:官方标配,频响范围:20-20000Hz,耳机售后服务:全国联保,型号:URBEATS,耳机输出音源:随身视听,缆线长度:1.2M,阻抗:32Ω,耳机类型:有线,耳机类别:HIFI耳机,生产企业:美国苹果公司,适用音乐类型:摇滚金属重低音类型,插头直径:3.5mm,有无麦克风:带麦,兼容平台:ANDROID iOS,佩戴方式:入耳式,颜色分类:哑光黑 土豪银 玫瑰金色 土豪金 天空灰 紫色 深灰,保修期:12个月,灵敏度:108dB/mW","brandName":"beats"}]};

    
    setTimeout(() => {

      // 合计
      this.monthSellResulet = this.util.getSellAllResult(this.monthSellResulet, res.data);

      this.vm.data = res.data;

      this.vm.loading = false;

    }, 100);


  }
}
